package com.txsj.scan.fragment;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.google.gson.Gson;
import com.txsj.scan.R;
import com.txsj.scan.activity.BindLabelActivity;
import com.txsj.scan.adapter.WarehouseDetailAdapter;
import com.txsj.scan.base.BaseFragment;
import com.txsj.scan.base.BasePresenter;
import com.txsj.scan.base.IView;
import com.txsj.scan.bean.EpcInfo;
import com.txsj.scan.bean.GoodsInfo;
import com.txsj.scan.presenter.GoodsListFragmentPresenter;
import com.txsj.scan.utils.GsonFactory;

import java.util.ArrayList;
import java.util.List;

public class GoodsListFragment extends BaseFragment<BasePresenter> implements IView {


    private View view;
    private RecyclerView recyclerView;
    private WarehouseDetailAdapter adapter;
    private List<GoodsInfo> list;

    @Override
    protected int layoutId() {
        return R.layout.fragment_useful_label;
    }

    @Override
    public void init() {
        initView();
        initData();
    }

    @Override
    protected BasePresenter providePresenter() {
        return new GoodsListFragmentPresenter(this);
    }

    private void initView() {
        recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
        adapter = new WarehouseDetailAdapter(activity);
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        recyclerView.setAdapter(adapter);
        adapter.setListener(new WarehouseDetailAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if (list.size() == 0 || list.size() < position)return;
                Intent intent = new Intent(activity, BindLabelActivity.class);
                intent.putExtra("data",new Gson().toJson(list.get(position)));
                startActivity(intent);
            }

            @Override
            public void onDelete(int position) {
                //删除本地商品
                list.remove(position);
                adapter.removeData(position);
            }
        });
    }

    /**
     * 初始化数据
     */
    private void initData() {
        list = new ArrayList<>();
        String args = getArguments().getString("args","");
        if (TextUtils.isEmpty(args)){
            return;
        }
        List<GoodsInfo> goodsInfoList = GsonFactory.getObjectList(args,GoodsInfo.class);
        for (GoodsInfo info:goodsInfoList){
            filterSample(info);
        }
        adapter.setData(list);
    }


    /**
     * 合并重复的数据
     * 1. 合并后进行count++
     * 2. 将当前EpcGoodsInfo的strEPC添加到已存在的goodsEpcInfo中
     * @param epcGoodsInfo 还未合并的goodsInfo
     */
    private void filterSample(GoodsInfo epcGoodsInfo){
        boolean isContains = false;
        if (null != epcGoodsInfo){
            if (list.size() >0) {
                long goodsId = epcGoodsInfo.getGoods_id();
                for (GoodsInfo info : list) {
                    if (goodsId == info.getGoods_id()) {
                        //count++
//                        long count = info.getCount();
//                        info.setCount(++count);
                        //合并strEPC
                        List<EpcInfo> strEpcList = info.getEpcList();
                        if (null == strEpcList)strEpcList = new ArrayList<>();
                        strEpcList.addAll(epcGoodsInfo.getEpcList());
                        info.setEpcList(strEpcList);
                        //设置变量若不存在进行添加
                        isContains = true;
                        break;
                    }
                }
            }
            if (!isContains) {
//                epcGoodsInfo.setCount(1);
                list.add(epcGoodsInfo);
            }
        }
    }

    /**
     * @return
     */
    public List<GoodsInfo> getGoodsList() {
        return list;
    }


    @Override
    public Context context() {
        return activity;
    }

    public void updateList(List<GoodsInfo> goodsInfoList) {
        list.clear();
        for (GoodsInfo info:goodsInfoList){
            filterSample(info);
        }
        adapter.setData(list);
    }
}
